﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BCCommon;
using BCData.TMS.FreightAddress;
using BCData.TMS.FreightContact;
using BCDto.TMS.FreightAddress;
using BCDto.Website.User;
using BCEntity.TMS.FreightAddress;
using BCService.Utils;

namespace BCService.TMS.FreightAddressService
{
    public class FreightAddressService : IFreightAddressService
    {
        #region 私有变量

        private readonly IFreightContactData freightContactData;
        private readonly IFreightAddressData freightAddressData;

        #endregion 私有变量

        #region 构造函数
        public FreightAddressService(IFreightContactData freightContactData, IFreightAddressData freightAddressData)
        {
            this.freightAddressData = freightAddressData;
            this.freightContactData = freightContactData;
        }

        #endregion 构造函数

        #region 公共方法

        public FreightAddressDto Add(UserDto user, FreightAddressRequestDto requestDto)
        {
            //1 检查地址是否存在
            if (freightAddressData.CheckAddressAsync(user.UserId, requestDto.CountyCode, requestDto.CityCode, requestDto.ProvinceCode, requestDto.Address).GetAwaiter().GetResult())
                throw new ArgumentException("用户货运地址已存在");
            //2 检查常用联系人是否存在
            CheckFreightContact(requestDto.FreightContactId);
            //3 新增
            var entity = requestDto.As<FreightAddressEntity>();
            entity.UserId = user.UserId;
            var result = freightAddressData.InsertAsync(entity).GetAwaiter().GetResult();
            if (result == null)
            {
                throw new ArgumentException("货运地址新增失败");
            }
            return result.As<FreightAddressDto>();
        }

        public int Update(long freightAddressId, FreightAddressRequestDto requestDto)
        {
            //1 检查货运常用地址是否存在
            CheckFreightAddress(freightAddressId);
            //2 检查常用联系人是否存在
            CheckFreightContact(requestDto.FreightContactId);
            //3 修改
            var entity = requestDto.As<FreightAddressEntity>();
            entity.FreightAddressId = freightAddressId;
            return freightAddressData.UpdateAsync(entity).GetAwaiter().GetResult();
        }

        public int UpdateUserCount(long freightAddressId)
        {
            //1 检查货运常用地址是否存在
            CheckFreightAddress(freightAddressId);
            //2 修改常用次数
            return freightAddressData.UpdateUserCountAsync(freightAddressId).GetAwaiter().GetResult();
        }

        public FreightAddressDto GetById(long freightAddressId)
        {
            var data = this.freightAddressData.GetByIdAsync(freightAddressId).GetAwaiter().GetResult();
            return data.As<FreightAddressDto>();
        }

        public List<FreightAddressDto> GetListByUserId(long userId)
        {
            var data = this.freightAddressData.GetByUserIdAsync(userId).GetAwaiter().GetResult();
            return data.As<List<FreightAddressDto>>();
        }

        public PageableList<FreightAddressDto> GetAll(int pageIndex, int pageSize, string userName, string phone)
        {
            var queryResult = freightAddressData.GetAllAsync(pageIndex, pageSize, userName, phone).GetAwaiter().GetResult();
            return new PageableList<FreightAddressDto>()
            {
                Count = queryResult.Item2,
                Items = queryResult.Item1.As<List<FreightAddressDto>>(),
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
        public FreightAddressDto Get(long userId, double longitude, double latitude)
        {
            var data = this.freightAddressData.GetAsync(userId, longitude, latitude).GetAwaiter().GetResult();
            return data.As<FreightAddressDto>();
        }
        public FreightAddressDto Get(long userId, string countyId, string communityId, string provinceId, string address)
        {
            var data = this.freightAddressData.GetAsync(userId, countyId, communityId, provinceId, address).GetAwaiter().GetResult();
            return data.As<FreightAddressDto>();
        }

        #endregion 公共方法

        #region 私有方法

        /// <summary>
        /// 检查常用联系人是否存在
        /// </summary>
        /// <param name="freightcontactId">货运地址Id</param>
        /// <returns></returns>
        public void CheckFreightContact(long freightcontactId)
        {
            if (!freightContactData.CheckFreightContactExistsAsync(freightcontactId).GetAwaiter().GetResult())
                throw new ArgumentException("常用联系人不存在");
        }

        /// <summary>
        /// 检查货运常用地址是否存在
        /// </summary>
        /// <param name="freightAddressId">货运地址Id</param>
        /// <returns></returns>
        public void CheckFreightAddress(long freightAddressId)
        {
            if (!freightAddressData.CheckAddressAsync(freightAddressId).GetAwaiter().GetResult())
                throw new ArgumentException("用户货运地址不存在");
        }

        #endregion 私有方法
    }
}